﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Consts;
using Sector7.Framework.DataDictionary.Agents;

namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// AddValueDomainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddValueDomainWindow : AddModeWindowBase
    {
        /// <summary>
        /// 创建 AddValueDomainWindow 窗口
        /// </summary>
        public AddValueDomainWindow()
        {
            InitializeComponent();

            FocusedControl = txbDomainId;
        }

        /// <summary>
        /// 获取或设置相关值域信息
        /// </summary>
        public S7_ValueDomain ValueDomain
        {
            get { return m_entity as S7_ValueDomain; }
            set { m_entity = value; }
        }

        public S7_DataGroup DataGroup
        {
            get { return m_dataGroup; }
            set { m_dataGroup = value; }
        }

        private S7_DataGroup m_dataGroup = null;

        protected override void InitializeForCreating()
        {
            txbDomainId.Text = string.Empty;
            txbDomainName.Text = string.Empty;

            m_entity = new S7_ValueDomain();
            ValueDomain.Items = new List<S7_ValueDomainItem>();

            ClientUtil.LoadItemsSource(dtgItems, ValueDomain.Items);
        }

        protected override void InitializeForUpdating()
        {
            txbDomainId.Text = ValueDomain.DomainId;
            txbDomainName.Text = ValueDomain.DomainName;
            ClientUtil.LoadItemsSource(dtgItems, ValueDomain.Items);

            btnContinue.Visibility = Visibility.Hidden;
            btnAddItem.Visibility = Visibility.Hidden;
            dtgItems.IsEnabled = false;

            base.InitializeForUpdating();
        }

        protected override void InitializeForOthers()
        {
            if (m_dataGroup != null)
            {
                txbGroup.Text = string.Format("{0} {1}", m_dataGroup.GroupIdentifier, m_dataGroup.GroupName);
            }

            base.InitializeForOthers();
        }

        protected override void AddData()
        {
            ValueDomain.DomainId = txbDomainId.Text;
            ValueDomain.DomainName = txbDomainName.Text;
            ValueDomain.GroupId = m_dataGroup.GroupId;

            ValueDomain.Items.ForEach(delegate(S7_ValueDomainItem item)
            {
                item.DomainId = ValueDomain.DomainId;
            });

            switch (m_crud)
            {
                case CrudType.Create:
                    ValueDomainServiceAgent.Add(ValueDomain);
                    break;
                case CrudType.Update:
                    bool isChanged = false;
                    EntityDeltaBase delta = GetDelta(C_PrimaryKeyName.VALUE_DOMAIN, ref isChanged);

                    if (isChanged)
                    {
                        ValueDomainServiceAgent.Set(delta);
                    }
                    break;
                default:
                    break;
            }

            base.AddData();
        }

        protected override void DoModify()
        {
            var selectedItem = dtgItems.SelectedItem as S7_ValueDomainItem;
            if (selectedItem != null)
            {
                AddValueDomainItemWindow modifyItemWindow = new AddValueDomainItemWindow();
                modifyItemWindow.ValueDomainItem = selectedItem;
                modifyItemWindow.InitializeWindow(CrudType.Update);
                modifyItemWindow.AddSuccess += new EventHandler(addItemWindow_AddSuccess);

                modifyItemWindow.ShowDialog();

                base.DoModify();
            }
        }

        protected override void DoDelete()
        {
            var selectedItem = dtgItems.SelectedItem as S7_ValueDomainItem;
            if (selectedItem != null)
            {
                MessageBoxResult result = Alert.ShowDialog(AlertType.Question, "是否确认删除选中项？");

                if (result == MessageBoxResult.Yes)
                {
                    ValueDomain.Items.Remove(selectedItem);
                    dtgItems.Items.Refresh();

                    base.DoDelete();
                }
            }
        }

        protected override void DoOpen()
        {
            AddValueDomainItemWindow addItemWindow = new AddValueDomainItemWindow();
            addItemWindow.InitializeWindow(CrudType.Create);
            addItemWindow.AddSuccess += new EventHandler(addItemWindow_AddSuccess);

            addItemWindow.ShowDialog();

            base.DoOpen();
        }

        protected override bool CheckData()
        {
            m_errors.Clear();

            string domainId = txbDomainId.Text;
            string domainName = txbDomainName.Text;

            if (string.IsNullOrEmpty(domainId))
            {
                m_errors.Add("值域编号", "不能为空！");
            }
            if (string.IsNullOrEmpty(domainName))
            {
                m_errors.Add("值域名称", "不能为空！");
            }
            if (ValueDomain.Items.Count == 0)
            {
                m_errors.Add("值域子项", "不能为空！");
            }

            return base.CheckData();
        }

        private void dtgItems_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                DoModify();
            }
        }

        void addItemWindow_AddSuccess(object sender, EventArgs e)
        {
            var addItemWindow = sender as AddValueDomainItemWindow;
            var newItem = addItemWindow.ValueDomainItem;

            switch (addItemWindow.Crud)
            {
                case CrudType.Create:
                    ValueDomain.Items.Add(newItem);
                    ClientUtil.LoadSelectedItem(dtgItems, newItem);
                    //注:继续添加已经执行过ResetWindow,ResetWindow会执行AutoAdd,故AutoAdd执行两次
                    //addItemWindow.ResetWindow();
                    break;
                case CrudType.Update:
                    ValueDomain.Items.Find(delegate(S7_ValueDomainItem __item)
                    {
                        if (__item.DomainItemId == newItem.DomainItemId)
                        {
                            __item = newItem;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    });
                    break;
                default:
                    break;
            }
        }

        private void btnAddItem_Click(object sender, RoutedEventArgs e)
        {
            DoOpen();
        }

        private void miModify_Click(object sender, RoutedEventArgs e)
        {
            DoModify();
        }

        private void miDelete_Click(object sender, RoutedEventArgs e)
        {
            DoDelete();
        }
    }
}
